package com.ssm.sport.controller;

import com.ssm.sport.entity.Category;
import com.ssm.sport.entity.Shipping;
import com.ssm.sport.service.CartService;
import com.ssm.sport.service.CategoryService;
import com.ssm.sport.service.ShippingService;
import com.ssm.sport.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 页面控制器，处理一些基本页面的跳转
 */
@Controller
public class PageController {

    @Autowired
    private CategoryService categoryService;
    
    @Autowired
    private CartService cartService;

    @Autowired
    private ShippingService shippingService;

    /**
     * 登录页面
     */
    @GetMapping("/login")
    public String login(Model model, HttpSession session) {
        // 如果已经登录，则重定向到首页
        if (session.getAttribute("user") != null) {
            return "redirect:/";
        }
        
        // 获取所有一级分类，用于导航栏
        List<Category> categories = categoryService.getTopCategories();
        model.addAttribute("categories", categories);
        
        return "login";
    }

    /**
     * 注册页面
     */
    @GetMapping("/register")
    public String register(Model model, HttpSession session) {
        // 如果已经登录，则重定向到首页
        if (session.getAttribute("user") != null) {
            return "redirect:/";
        }
        
        // 获取所有一级分类，用于导航栏
        List<Category> categories = categoryService.getTopCategories();
        model.addAttribute("categories", categories);
        
        return "register";
    }

    /**
     * 关于我们页面
     */
    @GetMapping("/about")
    public String about(Model model) {
        // 获取所有一级分类，用于导航栏
        List<Category> categories = categoryService.getTopCategories();
        model.addAttribute("categories", categories);
        
        return "about";
    }

    /**
     * 联系我们页面
     */
    @GetMapping("/contact")
    public String contact(Model model) {
        // 获取所有一级分类，用于导航栏
        List<Category> categories = categoryService.getTopCategories();
        model.addAttribute("categories", categories);
        
        return "contact";
    }

    /**
     * 常见问题页面
     */
    @GetMapping("/faq")
    public String faq(Model model) {
        // 获取所有一级分类，用于导航栏
        List<Category> categories = categoryService.getTopCategories();
        model.addAttribute("categories", categories);
        
        return "faq";
    }

    /**
     * 配送方式页面
     */
    @GetMapping("/shipping")
    public String shipping(Model model) {
        // 获取所有一级分类，用于导航栏
        List<Category> categories = categoryService.getTopCategories();
        model.addAttribute("categories", categories);
        
        return "shipping";
    }
    
    /**
     * 结算页面
     */
    @GetMapping("/checkout")
    public String checkout(Model model, HttpSession session) {
        // 如果未登录，则重定向到登录页面
        Integer userId = null;
        Object userObj = session.getAttribute("user");
        if (userObj == null) {
            return "redirect:/login";
        } else {
            userId = ((com.ssm.sport.entity.User) userObj).getId();
        }
        
        // 获取所有一级分类，用于导航栏
        List<Category> categories = categoryService.getTopCategories();
        model.addAttribute("categories", categories);
        
        // 获取购物车中已选中的商品
        List<CartVO> cartList = cartService.getCartVOList(userId);
        List<CartVO> checkedItems = cartList.stream()
                .filter(item -> item.getChecked() == 1)
                .collect(Collectors.toList());
        
        if (checkedItems.isEmpty()) {
            return "redirect:/cart";
        }
        
        // 计算总价
        BigDecimal totalPrice = checkedItems.stream()
                .map(CartVO::getProductTotalPrice)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        
        model.addAttribute("cartItems", checkedItems);
        model.addAttribute("totalPrice", totalPrice);
        
        // 获取收货地址列表
        List<Shipping> shippingAddresses = shippingService.getByUserId(userId);
        model.addAttribute("shippingAddresses", shippingAddresses);
        
        return "checkout";
    }
} 